<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>localStorage</title>
</head>
<body>
<div>
    <h2>localStorage</h2>
    <button onclick="saveData()">点我保存一个数据</button>
    <button onclick="readData()">点我读取一个数据</button>
    <button onclick="delData()">点我删除一个数据</button>
    <button onclick="clearData()">点我清空数据</button>
</div>
<script type="text/javascript">
    let person = {name:'xccit',age:21}
    function saveData(){
        //保存对象
        localStorage.setItem('person',JSON.stringify(person))
        //保存字符
        /*localStorage.setItem('msg','xccit')*/
        //保存数字(会自动转成string)
        /*localStorage.setItem('msg2',21)*/
    }
    function readData(){
        const p = JSON.parse(localStorage.getItem('person'))
        console.log(p);
        console.log(localStorage.getItem('msg'))
        console.log(localStorage.getItem('msg2'))
        console.log(localStorage.getItem('msg3')) //没有的值 读出来是null
    }
    function delData(){
        localStorage.removeItem('msg2')
    }
    function clearData(){
        localStorage.clear()
    }
</script>
</body>
</html>